﻿// Apache-2.0 License
// Copyright (c) 2021-2022 SuperJeff
// 微信:15906588664  QQ:1024489

using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Admin.Net.InformationPlatform.Service.JFJL;
/// <summary>
/// 定时读取任务
/// </summary>
public class HeartBeatService:IHostedService
{
    private Timer _timer;

    public Task StartAsync(CancellationToken cancellationToken)
    {
        _timer = new Timer(SendHeartbeat, null, TimeSpan.Zero, TimeSpan.FromHours(1));
        return Task.CompletedTask;
    }

    public Task StopAsync(CancellationToken cancellationToken)
    {
        _timer?.Dispose();
        return Task.CompletedTask;
    }

    private void SendHeartbeat(object state)
    {
        using (HttpClient client = new HttpClient())
        {
            var content = new StringContent("", Encoding.UTF8, "application/json");

            // 发送 POST 请求到 API 端点
            var response = client.PostAsync("http://10.8.1.151:8084/SRM/GetData", content).Result;

            // 检查响应状态码
            if (response.IsSuccessStatusCode)
            {
                Console.WriteLine("Heartbeat sent successfully");
            }
            else
            {
                Console.WriteLine("Failed to send heartbeat");
            }
        }
    }
}
